/*
 * Copyright 2008-2010 Daniel Cachapa <cachapa@gmail.com>
 * 
 * This program is distributed under the terms of the GNU General Public License Version 3
 * The license can be read in its entirety in the LICENSE.txt file accompanying this source code,
 * or at: http://www.gnu.org/copyleft/gpl.html
 * 
 * This file is part of Libra.
 *
 * WeightWatch is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * WeightWatch is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the WeightWatch source code. If not, see: http://www.gnu.org/licenses
 */

package net.cachapa.libra.data;

import android.content.Context;
import net.cachapa.libra.util.MyDate;
import net.cachapa.libra.util.UnitManager;

public class Value {
	private MyDate date;
	private float weight;
	private float trend;

	public Value(MyDate date, float weight, float trend) {
		this.date = date;
		this.weight = weight;
		this.trend = trend;
	}
	
	public Value(MyDate date, float weight) {
		this(date, weight, 0f);
	}
	
	public Value(String formatedString) throws IllegalArgumentException {
		String[] values = formatedString.split(";");
		if (values.length < 2) {
			throw new IllegalArgumentException("Malformed value string: " + formatedString + "\nExpected format: YYYY-MM-DD;Weight");
		}
		
		try {
			date = new MyDate(values[0]);
			this.weight = Float.valueOf(values[1]).floatValue();
		} catch (Exception e) {
			throw new IllegalArgumentException("Malformed value string: " + formatedString + "\nExpected format: YYYY-MM-DD;Weight");
		}
	}
	
	public MyDate getDate() {
		return date;
	}
	
	public void setDate(MyDate date) {
		this.date = date;
	}
	
	public float getWeight() {
		return weight;
	}
	
	public void setWeight(float weight) {
		this.weight = weight;
	}
	
	public float getTrend() {
		return trend;
	}
	
	public void setTrend(float trend) {
		this.trend = trend;
	}
	
	public int getId() {
		return date.getDate();
	}
	
	@Override
	public boolean equals(Object other) {
		Value otherValue = (Value)other;
		return this.date.equals(otherValue.getDate()) && this.weight == otherValue.getWeight();
	}
	
	public String toString() {
		return date.toString() + ": " + weight;
	}
	
	public String toString(Context context) {
		return date.toString() + ": " + UnitManager.getInstance(context).toWeightUnit(weight);
	}
	
	public String toFormatedString() {
		return date.toStandardString() + ";"
			+ weight + ";"
			+ trend;
	}
}
